package day10.supermarket;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Market
 * zhangJ
 * 2022/6/30 17:07
 **/
public class Market implements Serializable {
    public static final String PATH = "market.txt";
    private String name;
    private String addr;
    private List<Card> cardList = new ArrayList<Card>();


    public Market(String name, String addr) {
        this.name = name;
        this.addr = addr;
    }
    //从文件读取对象序列化
    public static Market getMarket(){
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PATH));
            Market market = (Market) ois.readObject();
            ois.close();
            return market;
        } catch (Exception e) {
            System.out.println("对象不存在");
            //???e.printStackTrace();
        }
        return null;
    }
    //对象写入文件反序列化
    public static void setMarket(Market market){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(PATH));
            oos.writeObject(market);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Market)) return false;
        Market market = (Market) o;
        return getName().equals(market.getName()) && getAddr().equals(market.getAddr());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAddr());
    }

    public Market() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public List<Card> getCardList() {
        return cardList;
    }

//    public void setCardList(List<Card> cardList) {
//        this.cardList = cardList;
//    }

    //添加会员
    public boolean add(Card c){
        System.out.println(c.getNum());
        if (!checkCardNoRepeat(c.getNum())) {
            cardList.add(new Card(c.getNum(), c.getJifen()));
            return true;
        }
        return false;
    }
    //判断会员号是否重复
    public boolean checkCardNoRepeat(String no){
        for (int i = 0; i < cardList.size(); i++) {
            if (no.equals(cardList.get(i).getNum())){
                System.out.println("会员号重复，");
                return true;
            }
        }
        return false;
    }
    //给用户涨分
    public boolean inc(String cardNo,int fen){
        for (int i = 0; i < cardList.size(); i++) {
            if(cardNo.equals(cardList.get(i).getNum())){
                Card card = cardList.get(i);
                card.setJifen(cardList.get(i).getJifen()+fen);
                cardList.set(i,card);
                return true;
            }
        }
        return false;
    }
    //兑换礼品
    public String duihuan(String cardNo,int lipinleixing){
        int i=0;
        for (; i < cardList.size(); i++) {
            if (cardNo.equals(cardList.get(i).getNum())){
                if(lipinleixing==1){
                    if (cardList.get(i).getJifen()>=100){
                        cardList.get(i).setJifen(cardList.get(i).getJifen()-100);
                        return "兑换餐巾纸成功";
                    }
                }else if(lipinleixing==2){
                    if (cardList.get(i).getJifen()>=400){
                        cardList.get(i).setJifen(cardList.get(i).getJifen()-400);
                        return "兑换剃须刀成功";
                    }
                }else if(lipinleixing==3){
                    if (cardList.get(i).getJifen()>=1000){
                        cardList.get(i).setJifen(cardList.get(i).getJifen()-1000);
                        return "兑换炒锅成功";
                    }
                }
            }
        }
        return null;

    }

    public void show(){//当前对象
        System.out.println(this.name+","+this.addr);
        List<Card> list = this.getCardList();
        for(Card c:list){
            System.out.println(c.getNum()+":"+c.getJifen());
        }
    }
}
